package com.dingwei.superiordiary.ui.activity;

import android.content.Intent;
import android.net.wifi.WifiEnterpriseConfig;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bobomee.android.mentions.edit.MentionEditText;
import com.bobomee.android.mentions.edit.util.FormatRangeManager;
import com.bobomee.android.mentions.text.MentionTextView;
import com.bumptech.glide.Glide;
import com.dingwei.superiordiary.R;
import com.dingwei.superiordiary.app.MyApplication;
import com.dingwei.superiordiary.bean.IndexAttentionBean;
import com.dingwei.superiordiary.bean.MyCarePersonalBean;
import com.dingwei.superiordiary.mvp.presenter.ForwardEditingPresenter;
import com.dingwei.superiordiary.mvp.view.Contract;
import com.dingwei.superiordiary.parser.Parser;
import com.lihao.baselibrary.base.BaseActivity;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;
import com.umeng.socialize.media.Base;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author 南柯一梦丶
 * Created time 2018/10/18.
 * Personal signature 余生还长 何必慌张
 * Page description :
 * .::::.
 * .::::::::.
 * :::::::::::
 * ..:::::::::::'
 * '::::::::::::'
 * .::::::::::
 * '::::::::::::::..
 * ..::::::::::::.
 * ``::::::::::::::::
 * ::::``:::::::::'        .:::.
 * ::::'   ':::::'       .::::::::.
 * .::::'      ::::     .:::::::'::::.
 * .:::'       :::::  .:::::::::' ':::::.
 * .::'        :::::.:::::::::'      ':::::.
 * .::'         ::::::::::::::'         ``::::.
 * ...:::           ::::::::::::'              ``::.
 * ```` ':.          ':::::::::'                  ::::..
 * '.:::::'                    ':'````..
 */
public class ForwardEditingActiivty extends BaseActivity implements Contract.ForwardEditingView {

    ForwardEditingPresenter forwardEditingPresenter = new ForwardEditingPresenter(this);

    @Bind(R.id.img_back)
    ImageView imgBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_right)
    TextView tvRight;
    @Bind(R.id.img_head)
    ImageView imgHead;
    @Bind(R.id.tv_nick_name)
    TextView tvNickName;
    @Bind(R.id.tv_level)
    TextView tvLevel;
    @Bind(R.id.tv_add_time)
    TextView tvAddTime;
    @Bind(R.id.tv_content)
    MentionTextView tvContent;
    @Bind(R.id.tv_posts_type)
    TextView tvPostsType;
    @Bind(R.id.img_size)
    TextView imgSize;
    @Bind(R.id.img_layout)
    LinearLayout imgLayout;
    @Bind(R.id.img_video)
    ImageView imgVideo;
    @Bind(R.id.et_content)
    MentionEditText etContent;
    @Bind(R.id.btn_submit)
    Button btnSubmit;

    private IndexAttentionBean.DataBean.ListBean bean;
    private FormatRangeManager mRangeManager;
    private Parser mTagParser = new Parser();
    @Override
    protected int getContentViewId() {
        return R.layout.activity_forward_editing;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {

        mRangeManager = new FormatRangeManager();
        tvContent.setMovementMethod(new LinkMovementMethod());
        tvContent.setParserConverter(mTagParser);
        tvContent.setHighlightColor(getResources().getColor(android.R.color.transparent));
        bean = (IndexAttentionBean.DataBean.ListBean) getIntent().getSerializableExtra("bean");


        if (bean.istranspond.equals("0")){
            Glide.with(BaseActivity.context)
                    .load(bean.portrait)
                    .apply(MyApplication.headOptions)
                    .into(imgHead);
            tvNickName.setText(bean.nickname);
            if (TextUtils.isEmpty(bean.video)){
                imgLayout.setVisibility(View.VISIBLE);
                imgVideo.setVisibility(View.GONE);
                imgSize.setText(""+bean.image.size());
            }else {
                imgVideo.setVisibility(View.VISIBLE);
                imgLayout.setVisibility(View.GONE);
            }
            String s = bean.content.replaceAll("'>", "'>@");
            tvContent.setText(mRangeManager.getFormatCharSequence(s));
            tvAddTime.setText(bean.addtime);
            tvLevel.setText("LV."+bean.level);
        }else {
            Glide.with(BaseActivity.context)
                    .load(bean.transpond_content.portrait)
                    .apply(MyApplication.headOptions)
                    .into(imgHead);
            tvNickName.setText(bean.transpond_content.nickname);
            if (TextUtils.isEmpty(bean.transpond_content.video)){
                imgLayout.setVisibility(View.VISIBLE);
                imgVideo.setVisibility(View.GONE);
                imgSize.setText(""+bean.transpond_content.image.size());
            }else {
                imgVideo.setVisibility(View.VISIBLE);
                imgLayout.setVisibility(View.GONE);
            }
            String s = bean.transpond_content.content.replaceAll("'>", "'>@");
            tvContent.setText(mRangeManager.getFormatCharSequence(s));
            tvAddTime.setText(bean.transpond_content.addtime);
            tvLevel.setText("LV."+bean.transpond_content.level);
        }


    }

    @Override
    protected void initListener() {
        etContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (count == 1 && !TextUtils.isEmpty(s)) {
                    char mentionChar = s.toString().charAt(start);
                    int selectionStart = etContent.getSelectionStart();
                    if (mentionChar == '@') {
                        intent = new Intent(BaseActivity.context, aitePersonalActivity.class);
                        intent.putExtra("userid", BaseActivity.value.getUserId());
                        startActivityForResult(intent, 1001);
                        etContent.getText().delete(selectionStart - 1, selectionStart);
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.img_back, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.btn_submit:
                CharSequence convertMetionString = etContent.getFormatCharSequence();
                if (bean.istranspond.equals("0")){
                    forwardEditingPresenter.forward(bean.id,convertMetionString.toString().trim());
                }else {
                    forwardEditingPresenter.forward(bean.transpond_content.id,convertMetionString.toString().trim());
                }
                break;
        }
    }

    @Override
    public void forwardSuccess() {
        finish();
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }
        if (requestCode == 1001) {
            MyCarePersonalBean.DataBean.ListBean user = (MyCarePersonalBean.DataBean.ListBean) data.getSerializableExtra("user");
            etContent.insert(user);
        }
    }
}
